// mixin to src/views/main/Rehearsal/index.vue
import { getTerrHightByDegree } from '../../tools'

export default {
    data() {
        return {
            idHD: '',
            stateHD: '',
            iconHD: require('@/assets/map/popHD/QL.png'),
            iconHD1: require('@/assets/map/popHD/QL_1.png'),
            iconHD2: require('@/assets/map/popHD/QL_2.png'),
            dtBeginHD: null,
            dtEndHD: null
        }
    },
    destroyed() {
        this.clearHiddenDanger()
        this.$eventBus.$off('HDDlgFormSubmit')
        this.$eventBus.$off('HDState')
        this.$eventBus.$off('HDDefaultTime')
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        }
    },
    watch: {
        basin: {
            handler: function (name) {
                this.$nextTick(() => {
                    this.initHD()
                })
            },
            immediate: true
        }
    },
    created() {
        this.entitiesHiddenDanger = []
        this.$eventBus.$on('HDDlgFormSubmit', this.changeHDDetailOne) // 对语框提交
        this.$eventBus.$on('HDState', this.setHDState)
        this.$eventBus.$on('HDDefaultTime', params => {
            this.dtBeginHD = params.dtBeginHD
            this.dtEndHD = params.dtEndHD
        })
        this.hdMap = new Map()
    },
    mounted() {
    },
    methods: {
        initHD() {
            this.clearHiddenDanger()
            this.stateHD = 'initing'
            this.$api.Get('/api/HiddenDanger/GetDPMap', { wswscd: this.basin }).then(res => {
                res.data.forEach(item => {
                    // {
                    //     "id": "581C620E-A9A1-DCE2-2450-F1B732EFF0E6",
                    //     "name": "保安镇1",
                    //     "lgtd": 34.15184399,
                    //     "lttd": 109.94935853,
                    //     "bridgelong": 73.2,
                    //     "bridgewidth": 8.5,
                    //     "bridgeheight": 8,
                    //     "pierwidth": 19.5,
                    //     "wswscd": "WDA8000126100000"
                    // }
                    var z1 = 0
                    var pos = new Cesium.CallbackProperty((time, result) => {
                        z1 += 0.05
                        var h2 = Math.sin(z1) * 10
                        return Cesium.Cartesian3.fromDegrees(
                            item.lgtd,
                            item.lttd,
                            20 + h2
                        )
                    })
                    var entity = {
                        eventName: '隐患名称',
                        lid: 'HiddenDanger',
                        prop: item,
                        position: pos,
                        billboard: {
                            image: this.iconHD,
                            scale: 0.5,
                            // color: Cesium.Color.fromCssColorString('#FF0000'),
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                            width: 41,
                            height: 65
                        }
                    }
                    entity = this.$viewer.entities.add(entity)
                    this.hdMap.set(item.id, entity)
                })
                this.stateHD = 'inited'
                this.initEvevtHiddenDanger()
            })
        },
        setHDState(option) {
            switch (option.action) {
                case '方案详情':
                    this.changeHDDetailList(option.list)
                    break
                case '创建方案':
                    this.changeHDDetailList([])
                    break
                case '取消创建':
                    this.changeHDDetailList([])
                    break
                case '删除以创建':
                    this.delHDDetailOne(option.id)
                    break
                case '无隐患':
                    this.changeHDDetailList([])
                    break
                case '隐患方案':
                    this.changeHDDetailList(option.list)
                    break
                case '自定义隐患':
                    this.hdCustom()
                    break
                case '隐患行选择':
                    this.flyToHD(option.id)
                    break
            }
        },
        changeHDDetailOne(form) {
            var entity = this.hdMap.get(form.id)
            if (entity) {
                entity.eventName = '隐患详情'
                entity.propConfig = form
                if (form.dstypeName === '半堵') {
                    // entity.billboard.color = Cesium.Color.fromCssColorString('#ffdb13')
                    entity.billboard.image = this.iconHD1
                } else if (form.dstypeName === '全堵') {
                    // entity.billboard.color = Cesium.Color.fromCssColorString('#FF0000')
                    entity.billboard.image = this.iconHD2
                }
            }
        },
        changeHDDetailList(list) {
            this.hdMap.forEach((entity, key) => {
                // 查找被设置了的隐患点的设置详情
                var config = list.find(item => {
                    return item.pointsid === key
                })
                if (config) {
                    entity.eventName = '隐患详情'
                    entity.propConfig = config
                    if (config.dstypeName === '半堵') {
                        entity.billboard.image = this.iconHD1
                    } else if (config.dstypeName === '全堵') {
                        entity.billboard.image = this.iconHD2
                    }
                } else {
                    entity.eventName = '隐患名称'
                    entity.billboard.image = this.iconHD
                }
            })
        },
        delHDDetailOne(id) {
            var entity = this.hdMap.get(id)
            if (entity) {
                entity.eventName = '隐患自定义'
                entity.billboard.image = this.iconHD
            }
        },
        flyToHD(id) {
            var entity = this.hdMap.get(id)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 2000
                    }
                })
            }
        },
        hdCustom() {
            this.hdMap.forEach((entity, key) => {
                entity.eventName = '隐患自定义'
                // entity.billboard.color = Cesium.Color.fromCssColorString('#FFFFFF')
                entity.billboard.image = this.iconHD
            })
        },
        initEvevtHiddenDanger() {
            var eventFn = pickedObject => {
                if (
                    pickedObject &&
                    pickedObject.id &&
                    pickedObject.id.eventName
                ) {
                    if (pickedObject.id.eventName === '隐患自定义') {
                        var lgtd = pickedObject.id.prop.lgtd
                        var lttd = pickedObject.id.prop.lttd
                        console.log(pickedObject)
                        getTerrHightByDegree(this.$viewer, lgtd, lttd).then(res => {
                            const popStayParams = this.$store.getters.popStayParams

                            // popStayParams.posX = Math.round(event.position.x)
                            // popStayParams.posY = Math.round(event.position.y)
                            popStayParams.remarks = pickedObject.id.prop
                            popStayParams.remarks.lgtd = lgtd
                            popStayParams.remarks.lttd = lttd
                            popStayParams.remarks.h = res
                            popStayParams.remarks.dtBeginHD = this.dtBeginHD
                            popStayParams.remarks.dtEndHD = this.dtEndHD
                            popStayParams.name = '隐患自定义'
                            this.$store.dispatch('popStayParams', popStayParams)
                        })
                    }
                }
            }
            this.$store.getters.eventCesiumOption.leftPickFns.set('隐患自定义', eventFn)
            window.UE_initEvevtHiddenDanger = function (res) {
                eventFn(res)
            }
        },
        clearHiddenDanger() {
            if (this.hdMap.size > 0) {
                this.hdMap.forEach((val, key) => {
                    this.$viewer.entities.remove(val)
                })
                this.hdMap = new Map()
            }
            this.$store.getters.eventCesiumOption.leftPickFns.delete('隐患自定义')
        }
    }
}
